<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="群名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入群名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column label="群ID" align="center" prop="chatId" />
      <el-table-column label="群名称" align="center" prop="title" />
      <el-table-column label="群类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bot_group_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="群账号" align="center" prop="userName" />

      <el-table-column label="操作" align="right" class-name="small-padding fixed-width" fixed="right"  width="100%">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type!=='channel'"
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleVerify(scope.row)"
            v-hasPermi="['bot:verify:edit']"
          >入群验证</el-button>
          <el-button
            v-if="scope.row.type!=='channel'"
            size="mini"
            type="text"
            icon="el-icon-magic-stick"
            @click="handleWelcome(scope.row)"
            v-hasPermi="['bot:welcome:query']"
          >入群欢迎</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-timer"
            @click="handleJob(scope.row)"
            v-hasPermi="['bot:job:list']"
          >定时任务</el-button>
          <el-button
            v-if="scope.row.type!=='channel'"
            size="mini"
            type="text"
            icon="el-icon-turn-off-microphone"
            @click="handleWordConfig(scope.row)"
            v-hasPermi="['bot:word:query']"
          >违禁词管理</el-button>
          <el-button
            v-if="scope.row.type!=='channel'"
            size="mini"
            type="text"
            icon="el-icon-chat-dot-round"
            @click="handleReply(scope.row)"
            v-hasPermi="['bot:reply:list']"
          >关键词回复</el-button>
          <el-button
            v-if="scope.row.type==='channel'"
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="handleMessageForward"
          >消息转发</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改群组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="群ID" prop="title">
          <el-input v-model="form.chatId" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="群名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="群账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入群名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 违禁词配置对话框 -->
    <el-dialog title="违禁词触发设置" :visible.sync="openDialog.word" width="500px" append-to-body>
      <el-form ref="formWord" :model="fromWord" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="状态" prop="status">
          <el-select v-model="fromWord.status" placeholder="是否启用检测">
            <el-option
              v-for="dict in dict.type.bot_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称检测" prop="status">
          <el-select v-model="fromWord.banNickName" placeholder="是否启用检测">
            <el-option
              v-for="dict in dict.type.bot_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="TG链接检测" prop="status">
          <el-select v-model="fromWord.banTelegramUrl" placeholder="是否启用检测">
            <el-option
              v-for="dict in dict.type.bot_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="封禁时长" prop="banTime">
          <el-select v-model="fromWord.banTime" placeholder="请选择封禁时长">
            <el-option
              v-for="dict in dict.type.bot_ban_time"
              :key="dict.value"
              :label="dict.label"
              :value='dict.value'
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="formWordManage" v-hasPermi="['bot:words:list']">管理违禁词</el-button>
        <el-button type="primary" @click="formWordSubmitForm">确 定</el-button>
        <el-button @click="openDialog.word = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGroup, delGroup, addGroup, updateGroup } from "@/api/bot/group";
import {getWord, updateWord} from "@/api/bot/word";

export default {
  name: "Group",
  dicts: ['bot_status','bot_ban_time','bot_group_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 群组表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDialog:{
        word:false
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
        userName: null,
        orderNum: null,
        status: null,
        createById: null,
      },
      // 表单参数
      form: {},
      fromWord:{
      },
      // 表单校验
      rules: {
        chatId: [
          { required: true, message: "群组ID不能为空", trigger: "blur" }
        ],
        banTime: [
          { required: true, message: "封禁时长不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询群组列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    resetFromWord() {
      this.fromWord = {
          chatId:null,
          banNickName:'0',
          banTelegramUrl:'0',
          banTime:null,
          status:'0',
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chatId:null,
        title: null,
        type: null,
        userName: null,
        remark: null,
        orderNum: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加群组";
    },
    /** 入群验证 */
    handleVerify(row) {
      this.$router.push({ path: "/tg/verifydetail",query:row }).catch(()=>{});
    },
    handleWelcome(row){
      this.$router.push({ path: "/tg/welcome",query:row }).catch(()=>{});
    },
    handleReply(row){
      this.$router.push({ path: "/tg/reply",query:row }).catch(()=>{});
    },
    handleJob(row){
      this.$router.push({ path: "/tg/job",query:row }).catch(()=>{});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除群组编号为"' + ids + '"的数据项？').then(function() {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bot/group/export', {
        ...this.queryParams
      }, `group_${new Date().getTime()}.xlsx`)
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          //this.handleResetPwd(row);
          break;
        case "handleAuthRole":
          //this.handleAuthRole(row);
          break;
        default:
          break;
      }
    },
    /**---------------------------------------------**/
    /*违禁词配置弹窗*/
    handleWordConfig(row){
      this.resetFromWord();
      this.form = row
      this.fromWord.chatId = row.chatId
      getWord(this.fromWord.chatId).then(response => {
        if(response.data){
          this.fromWord = response.data;
          this.fromWord.banTime = response.data.banTime+""
        }
        this.openDialog.word = true;
      });
    },
    //管理违禁词
    formWordManage(){
      this.$router.push({ path: "/tg/words",query:this.form }).catch(()=>{});
    },
    /**违禁词配置**/
    formWordSubmitForm(){
      console.log("this.forms",this.fromWord)
      this.$refs["formWord"].validate(valid => {
        if (valid) {
          updateWord(this.fromWord).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openDialog.word = false;
          });
        }
      });
    },
    handleMessageForward(){
      this.$alert('频道之间消息相互流转，开发中...', '消息转发', {
        confirmButtonText: '确定',
      });
    }
  /**---------------------------------------------**/
  }
};
</script>
